<?php
namespace App\Repository\User;

use App\Facade\Event;
use App\Event\User\MoneyDiscountEvent;

Trait MoneyDiscount
{
    // 使用
    public function moneyDiscountMake($money, $content)
    {
        if($this->moneyDiscountHas($money))
        {
            return $this->moneyDiscountAdd(-1 * $money, $content);
        }
    }
    
    // 判断
    public function moneyDiscountHas($money)
    {
        if($money > 0)
        {
            return $this->model->money_discount >= $money;
        }
    }
    
    // 增加
    public function moneyDiscountAdd($money, $content)
    {
        $res = $this->model->increment('money_discount', $money);
        
        Event::dispatch(new MoneyDiscountEvent($this, $money, $content));
            
        return $res;
    }
}